package com.qf.service.impl;

import com.qf.Dao.PlayRepository;
import com.qf.pojo.BaseResp;
import com.qf.pojo.Play;
import com.qf.service.PlayService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlayServiceImpl implements PlayService {
    @Autowired
    PlayRepository playRepository;
    public BaseResp playall(Integer page,Integer size){
        BaseResp baseResp = new BaseResp();
        Pageable pageRequest = PageRequest.of(page-1, size);

        Page<Play> all = playRepository.findAll(pageRequest);
        baseResp.setData(all.getContent());
        baseResp.setCode(200);
        baseResp.setTotal(all.getTotalElements());
        return baseResp;
    }

    @Override
    public BaseResp playadd(Play play) {
        BaseResp baseResp = new BaseResp();
        if(play.getId()==null){
            baseResp.setMgs("添加成功");
        }else{
            baseResp.setMgs("修改成功");
        }
        baseResp.setCode(200);
        Play play1 = playRepository.saveAndFlush(play);
        return baseResp;
    }

    @Override
    public BaseResp delete(Integer id) {
        playRepository.deleteById(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setMgs("删除成功");
        return baseResp;
    }

}
